//
//  BabyGenderView.m
//  BabyHelper
//
//  Created by  thomas on 11-6-26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BabyGenderView.h"


@implementation BabyGenderView
@synthesize genderPicker;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	self.title = NSLocalizedString(@"Set Gender", @"");
    [super viewDidLoad];
	
	UIBarButtonItem *saveItem = [[UIBarButtonItem alloc]
								 initWithTitle:NSLocalizedString(@"Done",@"")
								 style:UIBarButtonItemStyleBordered 
								 target:self
								 action:@selector(toggleSave)		
								 ];
	self.navigationItem.rightBarButtonItem = saveItem;
	[saveItem release];	
	
	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];	
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
	self.genderPicker = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated{
	
	Dao *dao = [Dao getInstance];
	BabyProfile *babyProfile = [[BabyProfile alloc]init];
	[dao LoadBabyProfile:babyProfile];
	
	if(babyProfile.Gender == BabyGenderFemale)
		[self.genderPicker selectRow:1 inComponent:0 animated:true];
	else 
		[self.genderPicker selectRow:2 inComponent:0 animated:true];

	[babyProfile release];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
	return 2;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	if (row == 1)
		return NSLocalizedString(@"Girl", @"");
	else
		return NSLocalizedString(@"Boy", @"");
}

-(void)toggleSave{
	NSUserDefaults *prefs   = [NSUserDefaults standardUserDefaults];
	
	if ([self.genderPicker selectedRowInComponent:0] == 1) {
		[prefs setInteger:BabyGenderFemale forKey:@"Gender"]; 	
	}else{
		[prefs setInteger:BabyGenderMale forKey:@"Gender"]; 	
	}
		
	[self.navigationController popViewControllerAnimated:true];		
}

@end
